package com.skplanet.oneplusonewindow.moimserver.controller.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Member;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_Moim;
import com.skplanet.oneplusonewindow.moimserver.domain.Moim_User;
import com.skplanet.oneplusonewindow.moimserver.service.MoimService;
import com.skplanet.oneplusonewindow.moimserver.service.UserService;


@Controller
public class UserJSONController {
	
	@Autowired
	private UserService userService;

	@Autowired
	private MoimService moimService;
	
	@Autowired
	private MessageSource messageSource;
	
	@ResponseBody
	@RequestMapping( value = { "/json/user/user", "/json/user/user.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Moim_User getUser(
			@RequestParam(value="id", required=true)long id
			) {
		
		Moim_User user = userService.getUserFromId(id);
		
		return user;
	}
	
	@ResponseBody
	@RequestMapping( value = { "/json/user/list", "/json/user/list.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public List<Moim_User> list() {
		
		return userService.getAllUser();
	}
			
	@ResponseBody	
	@RequestMapping( value = { "/json/user/signup", "/json/user/signup.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> signup(
			@RequestParam(value="name", required=true)String name,
			@RequestParam(value="nickName", required=true)String nickName,
			@RequestParam(value="passwd", required=true)String passwd) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();

		if(userService.nameExists(name) == true) {
			
			mapJSON.put( "result", "nameExists" );
		}
		if(userService.nicknameExists(nickName) == true) {
			
			mapJSON.put( "result", "nickNameExists" );
		}
		else {
			Moim_User user = new Moim_User();
			user.setName(name);
			user.setNickname(nickName);
			user.setPassword(passwd);
			userService.addUser(user);
			mapJSON.put( "result", "success" );
		}
		
		return mapJSON;
	}
	@ResponseBody	
	@RequestMapping( value = { "/json/user/loginCheck", "/json/user/loginCheck.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public HashMap<String, Object> loginCheck(
			@RequestParam(value="j_username", required=true)String name,
			@RequestParam(value="j_password", required=true)String password) {	
		
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();
		
		if(userService.nameExists(name)){
			Moim_User user = userService.getUserFromName(name);
			if(userService.checkEqualsPassword(user, password)) {
				mapJSON.put("result", "success");
				mapJSON.put("username", name);
				mapJSON.put("userid", user.getId());
				mapJSON.put("nickname", user.getNickname());
				return mapJSON;
			}
		}
		else {
			mapJSON.put("result", "notRegistered");
			return mapJSON;
		}
		
		mapJSON.put("result", "fail");
		
		return mapJSON;
	}
	
	
	@ResponseBody	
	@RequestMapping( value = { "/json/user/login", "/json/user/login.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public List<HashMap<String, Object>> login(
			@RequestParam(value="name", required=true)String name) {	
		
		List<HashMap<String, Object>> mapJSON = new ArrayList<HashMap< String, Object >>();
		
		Moim_User user = userService.getUserFromName(name);
		List<Moim_Moim> moims = user.getMoims();
		int size = moims.size();
		for(int i = 0 ; i < size ; i ++){
			Moim_Moim moim = moims.get(i);
			long id = user.getId();
			Moim_Member member = moimService.getMemberFromUserId(moim.getId(), id);
			String pos = member.getPosition();
			HashMap<String, Object> tmp = new HashMap<String, Object>();
			tmp.put("Moim", moim);
			tmp.put("pos", pos);
			mapJSON.add(tmp);
		}
		
		return mapJSON;
	}
	@ResponseBody
	@RequestMapping( value = { "/json/user/addMoim", "/json/user/addMoim.json" }, method = RequestMethod.GET, headers = { "Accept=application/json"} )
	public Map<String, Object> addMoim(
			@RequestParam(value="name", required=true)String name,
			@RequestParam(value="moimId", required=true)long moimId,
			@RequestParam(value="pos", required=true)String pos
			) {
		HashMap<String, Object> mapJSON = new HashMap<String,Object>();
		Moim_User user = userService.getUserFromName(name);
		if(moimService.getMemberFromUserId(moimId, user.getId()) != null) {
			mapJSON.put("result", "exists");
			return mapJSON;
		}
		user = moimService.addMoim(name, moimId);
		Moim_Member member = new Moim_Member();
		member.setPosition(pos);
		member.setUserId(user.getId());
		moimService.addMember(moimId, member);
		if(user.getMoim(moimId) == moimService.getMoim(moimId))
			mapJSON.put("result", "success");
		else 
			mapJSON.put("result", "fail");
		return mapJSON;
	}
	@ResponseBody
	@RequestMapping( value = { "/json/user/deleteMoim", "/json/user/deleteMoim.json" }, method = RequestMethod.GET, headers = { "Accept=application/json"} )
	public Map<String, Object> deleteMoim(
			@RequestParam(value="name", required=true)String name,
			@RequestParam(value="moimId", required=true)long moimId
			) {
		HashMap<String, Object> mapJSON = new HashMap<String,Object>();
		long userId = userService.getUserIdFromName(name);
		Moim_Member member = moimService.getMemberFromUserId(moimId, userId);
		moimService.deleteMember(moimId, member);
		
		return mapJSON;
	}
	
	@ResponseBody
	@RequestMapping( value = { "/json/user/logged", "/json/user/logged.json" }, method = RequestMethod.GET, headers = { "Accept=application/json" } )
	public Map<String, Object> loggedUser() {
					
		HashMap<String, Object> mapJSON = new HashMap< String, Object >();
	
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		String name = auth.getName(); //get logged in username
		 
		Moim_User user = userService.getUserFromName(name);
		if(user == null) {
			mapJSON.put("result", "please signup");
		}
		else {
			mapJSON.put("loggedUserName", name);
			mapJSON.put("loggedUserId", userService.getUserIdFromName(name));
		}
		return mapJSON;
	}
}